﻿using System;
using System.Linq;
using System.Web.UI;
using Cotrobaie.UsersWebService;
using System.Web.UI.WebControls;

namespace Cotrobaie
{
    public partial class AdminEditUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBoxSearchUser.Attributes.Add("onChange", "onSearchTextChanged(this)");
            TextBoxSearchUser.Attributes.Add("onFocus", "textBoxSearch_onFocus(this)");
            TextBoxSearchUser.Attributes.Add("onBlur", "onSearchFocusOut(this)");
            TextBoxSearchUser.Attributes.Add("onKeyUp", "onKeyUpUser(this)");

            if (!Page.IsPostBack) Session["CommentsCurrPage"] = null;
            if (Session["AdminLogat"] == null) Response.Redirect("Home.aspx");
            if (AdminComments.Visible == true) PrintComments();

            if (!Page.IsPostBack) SuspendUnsuspend();
        }

        private void SuspendUnsuspend()
        {
            var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
            if (v != null)
            {
                if (v.AccountIsSuspended) ButtonLog.Text = "Un-Ban";
                else ButtonLog.Text = "Ban";
                if (v.AccountIsTempDeleted) { Button2.Enabled = false; Button2.Text = "Deleted"; }
                else Button2.Enabled = true; Button2.Text = "Delete";
            }
        }
        private void PrintComments()
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                int intrariPagina = 5;
                if (Session["CommentsCurrPage"] == null) Session["CommentsCurrPage"] = 0;
                var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
                if (v != null)
                {
                    var result = serviciu.GetUserComments(v.ID, v.Username, v.Parola, (int)Session["CommentsCurrPage"], intrariPagina);
                    if (result.Commenturi.Count() == 0 && (int)Session["CommentsCurrPage"] == 0)
                    {
                        Literal1.Text = "No comments added yet.";
                        ButtonNextPage.Visible = false;
                        ButtonPrevPage.Visible = false;
                    }
                    else
                    {
                        PlaceHolder1.Controls.Clear();
                        Table table = new Table();
                        PlaceHolder1.Controls.Add(table);

                        if (result.Commenturi.Count() < intrariPagina) { ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3"; }
                        if ((int)Session["CommentsCurrPage"] == 0) { ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3"; }
                        LabelCurrentPage.Text = ((int)Session["CommentsCurrPage"] + 1).ToString();

                        foreach (var c in result.Commenturi)
                        {
                            var res = serviciu.GetProdusByID(c.product_id);
                            var produs = res.Produse.First();
                            TableRow tr = new TableRow();
                            tr.Style.Add("width", "400px");
                            TableCell tc = new TableCell();
                            Label lab = new Label();

                            tc = new TableCell();
                            HyperLink hp = new HyperLink();
                            hp.Text = "<b>" + c.titlu + "</b>  -  " + produs.name;
                            hp.NavigateUrl = produs.linkSite;
                            hp.CssClass = "blgrlink";
                            tc.Controls.Add(hp);

                            tc.Style.Add("padding-right", "3px");
                            tr.Cells.Add(tc);

                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = c.data.ToString();
                            tc.ColumnSpan = 2;
                            tc.Controls.Add(lab);
                            tr.Cells.Add(tc);

                            table.Rows.Add(tr);

                            tr = new TableRow();

                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = c.comment;
                            tc.Controls.Add(lab);
                            tr.Cells.Add(tc);
                            tc.Style.Add("width", "300px");


                            tc = new TableCell();
                            Button but = new Button();
                            but.Text = "Edit";
                            but.CssClass = "bigbutton";
                            but.Click += onClickEdit;
                            but.CommandName = "edit";
                            but.Attributes.Add("text-align", "right");
                            but.CommandArgument = c.ID.ToString();

                            tc.Controls.Add(but);
                            tr.Cells.Add(tc);
                            tc = new TableCell();

                            Button but2 = new Button();
                            but2.Text = "Delete";
                            but2.CssClass = "bigbutton";
                            but2.Click += onClickRemove;
                            but2.CommandName = "delete";
                            but2.CommandArgument = c.ID.ToString();

                            tc.Controls.Add(but2);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);

                            //empty row
                            tr = new TableRow();
                            tc = new TableCell();
                            lab = new Label();
                            lab.Text = "&nbsp;";
                            tc.Controls.Add(lab);
                            tr.Cells.Add(tc);
                            table.Rows.Add(tr);
                        }

                    }
                }
            }
        }
        public void onClickRemove(object sender, EventArgs e)
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                Button but = (Button)sender;
                var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
                if (v != null)
                {
                    var result = serviciu.RemoveUserComment(v.ID, v.Username, v.Parola, int.Parse(but.CommandArgument));
                    if (result.Success) Page_Load(null, null);
                }
            }
        }
        public void onClickEdit(object sender, EventArgs e)
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                Button but = (Button)sender;
                var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
                if (v != null)
                {
                    var result = serviciu.GetUserComment(v.ID, v.Username, v.Parola, int.Parse(but.CommandArgument));
                    if (result.Success)
                    {
                        var comm=result.Commenturi.First();
                        var result2=serviciu.GetProdusByID(comm.product_id);
                        if (result2.Success)
                        {
                            var prod = result2.Produse.First();
                            TabelEditComment.Visible = true;
                            EditProductName.InnerHtml = "<a hef='" + prod.linkSite + "' class='blgrlink'>" + prod.name + "</a>";
                            EditProductDate.InnerHtml = String.Format("{0:dd.MM.yyyy}", comm.data);
                            TextBoxEditComment.Text = comm.comment;
                            TextBoxEditTitle.Text = comm.titlu;
                            EditCommentForEver.CommandArgument = but.CommandArgument;
                        }
                    }
                }
            }
        }

        public void showComments(object sender, EventArgs e)
        {
            AdminComments.Visible = true;
            PrintComments();
        }

        protected void logout_Click(object sender, EventArgs e)
        {
            Session["AdminLogat"] = null;
            Response.Redirect("Home.aspx");
        }

        protected void ButtonLog_Click(object sender, EventArgs e)
        {
            TabelEditComment.Visible = AdminComments.Visible = false;
            var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
            if (v != null)
                if (ButtonLog.Text == "Ban") if (!NonPublic.SuspendUser(v.ID).Success) return;
                    else ButtonLog.Text = "Un-Ban";
                else if (ButtonLog.Text == "Un-Ban") if (!NonPublic.UnSuspendUser(v.ID).Success) return;
                    else ButtonLog.Text = "Ban";

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TabelEditComment.Visible = AdminComments.Visible = false;
            var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
            if (v != null)
                if(!NonPublic.DeleteUser(v.ID).Success)return;
            Button2.Enabled = false; Button2.Text = "Deleted";
        }

        protected void ImageButton1_Click(object sender, EventArgs e)
        {
            try
            {
                TabelEditComment.Visible = false;
                AdminComments.Visible = false;
                var v = NonPublic.GetUser(TextBoxSearchUser.Text.Substring(0, TextBoxSearchUser.Text.IndexOf("#") - 1).Trim());
                if (v != null)
                {
                    Username1.InnerHtml = v.Username;
                    tabelUser.Visible = true;
                }
                else Username1.InnerHtml = "Unknown user";
            }
            catch { Username1.InnerHtml = "Unknown user"; }
        }

        protected void ButtonPrevPage_Click(object sender, EventArgs e)
        {
            if (Session["CommentsCurrPage"] != null)
                Session["CommentsCurrPage"] = (int)Session["CommentsCurrPage"] - 1;
            else Session["CommentsCurrPage"] = 0;
            ButtonNextPage.Enabled = true;
            ButtonNextPage.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void ButtonNextPage_Click(object sender, EventArgs e)
        {
            if (Session["CommentsCurrPage"] != null)
                Session["CommentsCurrPage"] = (int)Session["CommentsCurrPage"] + 1;
            else Session["CommentsCurrPage"] = 1;
            ButtonPrevPage.Enabled = true;
            ButtonPrevPage.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void EditCommentForEver_Click(object sender, EventArgs e)
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                Button but = (Button)sender;
                var v = NonPublic.GetUser(Username1.InnerHtml.Trim());
                if (v != null)
                {
                    var result = serviciu.EditUserComment(v.ID, v.Username, v.Parola, int.Parse(but.CommandArgument), TextBoxEditComment.Text, TextBoxEditTitle.Text);
                    if (result.Success)
                    {
                        EditProductDate.InnerHtml = EditProductName.InnerHtml = TextBoxEditTitle.Text = TextBoxEditComment.Text = String.Empty;
                        EditCommentForEver.CommandArgument = String.Empty;
                        TabelEditComment.Visible = false;
                        Page_Load(null, null);
                    }
                }
            }
        }


        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }
    }
}